<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>tracer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_debug_tracer.js.html">tracer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Definition of the Tracer class and associated classes.
<a name="line17"></a> *
<a name="line18"></a> * @see ../demos/tracer.html
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.debug.Trace&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.array&#39;);
<a name="line24"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line25"></a>goog.require(&#39;goog.iter&#39;);
<a name="line26"></a>goog.require(&#39;goog.structs.Map&#39;);
<a name="line27"></a>goog.require(&#39;goog.structs.SimplePool&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * Class used for singleton goog.debug.Trace.  Used for timing slow points in
<a name="line33"></a> * the code. Based on the java Tracer class but optimized for javascript.
<a name="line34"></a> * See com.google.common.base.Tracer.
<a name="line35"></a> * @constructor
<a name="line36"></a> * @private
<a name="line37"></a> */
<a name="line38"></a>goog.debug.Trace_ = function() {
<a name="line39"></a>
<a name="line40"></a>  /**
<a name="line41"></a>   * Events in order.
<a name="line42"></a>   * @type {Array.&lt;goog.debug.Trace_.Event_&gt;}
<a name="line43"></a>   * @private
<a name="line44"></a>   */
<a name="line45"></a>  this.events_ = [];
<a name="line46"></a>
<a name="line47"></a>  /**
<a name="line48"></a>   * Outstanding events that have started but haven&#39;t yet ended. The keys are
<a name="line49"></a>   * numeric ids and the values are goog.debug.Trace_.Event_ objects.
<a name="line50"></a>   * @type {goog.structs.Map}
<a name="line51"></a>   * @private
<a name="line52"></a>   */
<a name="line53"></a>  this.outstandingEvents_ = new goog.structs.Map();
<a name="line54"></a>
<a name="line55"></a>  /**
<a name="line56"></a>   * Start time of the event trace
<a name="line57"></a>   * @type {number}
<a name="line58"></a>   * @private
<a name="line59"></a>   */
<a name="line60"></a>  this.startTime_ = 0;
<a name="line61"></a>
<a name="line62"></a>  /**
<a name="line63"></a>   * Cummulative overhead of calls to startTracer
<a name="line64"></a>   * @type {number}
<a name="line65"></a>   * @private
<a name="line66"></a>   */
<a name="line67"></a>  this.tracerOverheadStart_ = 0;
<a name="line68"></a>
<a name="line69"></a>  /**
<a name="line70"></a>   * Cummulative overhead of calls to endTracer
<a name="line71"></a>   * @type {number}
<a name="line72"></a>   * @private
<a name="line73"></a>   */
<a name="line74"></a>  this.tracerOverheadEnd_ = 0;
<a name="line75"></a>
<a name="line76"></a>  /**
<a name="line77"></a>   * Cummulative overhead of calls to addComment
<a name="line78"></a>   * @type {number}
<a name="line79"></a>   * @private
<a name="line80"></a>   */
<a name="line81"></a>  this.tracerOverheadComment_ = 0;
<a name="line82"></a>
<a name="line83"></a>  /**
<a name="line84"></a>   * Keeps stats on different types of tracers. The keys are strings and the
<a name="line85"></a>   * values are goog.debug.Stat
<a name="line86"></a>   * @type {goog.structs.Map}
<a name="line87"></a>   * @private
<a name="line88"></a>   */
<a name="line89"></a>  this.stats_ = new goog.structs.Map();
<a name="line90"></a>
<a name="line91"></a>  /**
<a name="line92"></a>   * Total number of traces created in the trace.
<a name="line93"></a>   * @type {number}
<a name="line94"></a>   * @private
<a name="line95"></a>   */
<a name="line96"></a>  this.tracerCount_ = 0;
<a name="line97"></a>
<a name="line98"></a>  /**
<a name="line99"></a>   * Total number of comments created in the trace.
<a name="line100"></a>   * @type {number}
<a name="line101"></a>   * @private
<a name="line102"></a>   */
<a name="line103"></a>  this.commentCount_ = 0;
<a name="line104"></a>
<a name="line105"></a>  /**
<a name="line106"></a>   * Next id to use for the trace.
<a name="line107"></a>   * @type {number}
<a name="line108"></a>   * @private
<a name="line109"></a>   */
<a name="line110"></a>  this.nextId_ = 1;
<a name="line111"></a>
<a name="line112"></a>  /**
<a name="line113"></a>   * A pool for goog.debug.Trace_.Event_ objects so we don&#39;t keep creating and
<a name="line114"></a>   * garbage collecting these (which is very expensive in IE6).
<a name="line115"></a>   * @type {goog.structs.SimplePool}
<a name="line116"></a>   * @private
<a name="line117"></a>   */
<a name="line118"></a>  this.eventPool_ = new goog.structs.SimplePool(0, 4000);
<a name="line119"></a>  this.eventPool_.createObject = function() {
<a name="line120"></a>    return new goog.debug.Trace_.Event_();
<a name="line121"></a>  };
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>  /**
<a name="line125"></a>   * A pool for goog.debug.Trace_.Stat_ objects so we don&#39;t keep creating and
<a name="line126"></a>   * garbage collecting these (which is very expensive in IE6).
<a name="line127"></a>   * @type {goog.structs.SimplePool}
<a name="line128"></a>   * @private
<a name="line129"></a>   */
<a name="line130"></a>  this.statPool_ = new goog.structs.SimplePool(0, 50);
<a name="line131"></a>  this.statPool_.createObject = function() {
<a name="line132"></a>    return new goog.debug.Trace_.Stat_();
<a name="line133"></a>  };
<a name="line134"></a>
<a name="line135"></a>  var that = this;
<a name="line136"></a>  this.idPool_ = new goog.structs.SimplePool(0, 2000);
<a name="line137"></a>
<a name="line138"></a>  // TODO(nicksantos): SimplePool is supposed to only return objects.
<a name="line139"></a>  // Reconcile this so that we don&#39;t have to cast to number below.
<a name="line140"></a>  this.idPool_.createObject = function() {
<a name="line141"></a>    return String(that.nextId_++);
<a name="line142"></a>  };
<a name="line143"></a>  this.idPool_.disposeObject = function(obj) {};
<a name="line144"></a>
<a name="line145"></a>  /**
<a name="line146"></a>   * Default threshold below which a tracer shouldn&#39;t be reported
<a name="line147"></a>   * @type {number}
<a name="line148"></a>   * @private
<a name="line149"></a>   */
<a name="line150"></a>  this.defaultThreshold_ = 3;
<a name="line151"></a>};
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>/**
<a name="line155"></a> * Logger for the tracer
<a name="line156"></a> * @type {goog.debug.Logger}
<a name="line157"></a> * @private
<a name="line158"></a> */
<a name="line159"></a>goog.debug.Trace_.prototype.logger_ =
<a name="line160"></a>    goog.debug.Logger.getLogger(&#39;goog.debug.Trace&#39;);
<a name="line161"></a>
<a name="line162"></a>
<a name="line163"></a>/**
<a name="line164"></a> * Maximum size of the trace before we discard events
<a name="line165"></a> * @type {number}
<a name="line166"></a> */
<a name="line167"></a>goog.debug.Trace_.prototype.MAX_TRACE_SIZE = 1000;
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * Event type supported by tracer
<a name="line172"></a> * @enum {number}
<a name="line173"></a> */
<a name="line174"></a>goog.debug.Trace_.EventType = {
<a name="line175"></a>  /**
<a name="line176"></a>   * Start event type
<a name="line177"></a>   */
<a name="line178"></a>  START: 0,
<a name="line179"></a>
<a name="line180"></a>  /**
<a name="line181"></a>   * Stop event type
<a name="line182"></a>   */
<a name="line183"></a>  STOP: 1,
<a name="line184"></a>
<a name="line185"></a>  /**
<a name="line186"></a>   * Comment event type
<a name="line187"></a>   */
<a name="line188"></a>  COMMENT: 2
<a name="line189"></a>};
<a name="line190"></a>
<a name="line191"></a>
<a name="line192"></a>
<a name="line193"></a>/**
<a name="line194"></a> * Class to keep track of a stat of a single tracer type. Stores the count
<a name="line195"></a> * and cumulative time.
<a name="line196"></a> * @constructor
<a name="line197"></a> * @private
<a name="line198"></a> */
<a name="line199"></a>goog.debug.Trace_.Stat_ = function() {
<a name="line200"></a>  /**
<a name="line201"></a>   * Number of tracers
<a name="line202"></a>   * @type {number}
<a name="line203"></a>   */
<a name="line204"></a>  this.count = 0;
<a name="line205"></a>
<a name="line206"></a>  /**
<a name="line207"></a>   * Cumulative time of traces
<a name="line208"></a>   * @type {number}
<a name="line209"></a>   */
<a name="line210"></a>  this.time = 0;
<a name="line211"></a>
<a name="line212"></a>  /**
<a name="line213"></a>   * Total number of allocations for this tracer type
<a name="line214"></a>   * @type {number}
<a name="line215"></a>   */
<a name="line216"></a>  this.varAlloc = 0;
<a name="line217"></a>};
<a name="line218"></a>
<a name="line219"></a>
<a name="line220"></a>/**
<a name="line221"></a> * @type {string|null|undefined}
<a name="line222"></a> */
<a name="line223"></a>goog.debug.Trace_.Stat_.prototype.type;
<a name="line224"></a>
<a name="line225"></a>
<a name="line226"></a>/**
<a name="line227"></a> * @return {string} A string describing the tracer stat.
<a name="line228"></a> * @override
<a name="line229"></a> */
<a name="line230"></a>goog.debug.Trace_.Stat_.prototype.toString = function() {
<a name="line231"></a>  var sb = [];
<a name="line232"></a>  sb.push(this.type, &#39; &#39;, this.count, &#39; (&#39;, Math.round(this.time * 10) / 10,
<a name="line233"></a>      &#39; ms)&#39;);
<a name="line234"></a>  if (this.varAlloc) {
<a name="line235"></a>    sb.push(&#39; [VarAlloc = &#39;, this.varAlloc, &#39;]&#39;);
<a name="line236"></a>  }
<a name="line237"></a>  return sb.join(&#39;&#39;);
<a name="line238"></a>};
<a name="line239"></a>
<a name="line240"></a>
<a name="line241"></a>
<a name="line242"></a>/**
<a name="line243"></a> * Private class used to encapsulate a single event, either the start or stop
<a name="line244"></a> * of a tracer.
<a name="line245"></a> * @constructor
<a name="line246"></a> * @private
<a name="line247"></a> */
<a name="line248"></a>goog.debug.Trace_.Event_ = function() {
<a name="line249"></a>  // the fields are different for different events - see usage in code
<a name="line250"></a>};
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>/**
<a name="line254"></a> * @type {string|null|undefined}
<a name="line255"></a> */
<a name="line256"></a>goog.debug.Trace_.Event_.prototype.type;
<a name="line257"></a>
<a name="line258"></a>
<a name="line259"></a>/**
<a name="line260"></a> * Returns a formatted string for the event.
<a name="line261"></a> * @param {number} startTime The start time of the trace to generate relative
<a name="line262"></a> * times.
<a name="line263"></a> * @param {number} prevTime The completion time of the previous event or -1.
<a name="line264"></a> * @param {string} indent Extra indent for the message
<a name="line265"></a> *     if there was no previous event.
<a name="line266"></a> * @return {string} The formatted tracer string.
<a name="line267"></a> */
<a name="line268"></a>goog.debug.Trace_.Event_.prototype.toTraceString = function(startTime, prevTime,
<a name="line269"></a>    indent) {
<a name="line270"></a>  var sb = [];
<a name="line271"></a>
<a name="line272"></a>  if (prevTime == -1) {
<a name="line273"></a>    sb.push(&#39;    &#39;);
<a name="line274"></a>  } else {
<a name="line275"></a>    sb.push(goog.debug.Trace_.longToPaddedString_(this.eventTime - prevTime));
<a name="line276"></a>  }
<a name="line277"></a>
<a name="line278"></a>  sb.push(&#39; &#39;, goog.debug.Trace_.formatTime_(this.eventTime - startTime));
<a name="line279"></a>  if (this.eventType == goog.debug.Trace_.EventType.START) {
<a name="line280"></a>    sb.push(&#39; Start        &#39;);
<a name="line281"></a>  } else if (this.eventType == goog.debug.Trace_.EventType.STOP) {
<a name="line282"></a>    sb.push(&#39; Done &#39;);
<a name="line283"></a>    var delta = this.stopTime - this.startTime;
<a name="line284"></a>    sb.push(goog.debug.Trace_.longToPaddedString_(delta), &#39; ms &#39;);
<a name="line285"></a>  } else {
<a name="line286"></a>    sb.push(&#39; Comment      &#39;);
<a name="line287"></a>  }
<a name="line288"></a>
<a name="line289"></a>  sb.push(indent, this);
<a name="line290"></a>  if (this.totalVarAlloc &gt; 0) {
<a name="line291"></a>    sb.push(&#39;[VarAlloc &#39;, this.totalVarAlloc, &#39;] &#39;);
<a name="line292"></a>  }
<a name="line293"></a>  return sb.join(&#39;&#39;);
<a name="line294"></a>};
<a name="line295"></a>
<a name="line296"></a>
<a name="line297"></a>/**
<a name="line298"></a> * @return {string} A string describing the tracer event.
<a name="line299"></a> * @override
<a name="line300"></a> */
<a name="line301"></a>goog.debug.Trace_.Event_.prototype.toString = function() {
<a name="line302"></a>  if (this.type == null) {
<a name="line303"></a>    return this.comment;
<a name="line304"></a>  } else {
<a name="line305"></a>    return &#39;[&#39; + this.type + &#39;] &#39; + this.comment;
<a name="line306"></a>  }
<a name="line307"></a>};
<a name="line308"></a>
<a name="line309"></a>
<a name="line310"></a>/**
<a name="line311"></a> * Add the ability to explicitly set the start time. This is useful for example
<a name="line312"></a> * for measuring initial load time where you can set a variable as soon as the
<a name="line313"></a> * main page of the app is loaded and then later call this function when the
<a name="line314"></a> * Tracer code has been loaded.
<a name="line315"></a> * @param {number} startTime The start time to set.
<a name="line316"></a> */
<a name="line317"></a>goog.debug.Trace_.prototype.setStartTime = function(startTime) {
<a name="line318"></a>  this.startTime_ = startTime;
<a name="line319"></a>};
<a name="line320"></a>
<a name="line321"></a>
<a name="line322"></a>/**
<a name="line323"></a> * Initializes and resets the current trace
<a name="line324"></a> * @param {number} defaultThreshold The default threshold below which the
<a name="line325"></a> * tracer output will be supressed. Can be overridden on a per-Tracer basis.
<a name="line326"></a> */
<a name="line327"></a>goog.debug.Trace_.prototype.initCurrentTrace = function(defaultThreshold) {
<a name="line328"></a>  this.reset(defaultThreshold);
<a name="line329"></a>};
<a name="line330"></a>
<a name="line331"></a>
<a name="line332"></a>/**
<a name="line333"></a> * Clears the current trace
<a name="line334"></a> */
<a name="line335"></a>goog.debug.Trace_.prototype.clearCurrentTrace = function() {
<a name="line336"></a>  this.reset(0);
<a name="line337"></a>};
<a name="line338"></a>
<a name="line339"></a>
<a name="line340"></a>/**
<a name="line341"></a> * Resets the trace.
<a name="line342"></a> * @param {number} defaultThreshold The default threshold below which the
<a name="line343"></a> * tracer output will be supressed. Can be overridden on a per-Tracer basis.
<a name="line344"></a> */
<a name="line345"></a>goog.debug.Trace_.prototype.reset = function(defaultThreshold) {
<a name="line346"></a>  this.defaultThreshold_ = defaultThreshold;
<a name="line347"></a>
<a name="line348"></a>  for (var i = 0; i &lt; this.events_.length; i++) {
<a name="line349"></a>    var id = /** @type {Object} */ (this.eventPool_).id;
<a name="line350"></a>    if (id) {
<a name="line351"></a>      this.idPool_.releaseObject(id);
<a name="line352"></a>    }
<a name="line353"></a>    this.eventPool_.releaseObject(this.events_[i]);
<a name="line354"></a>  }
<a name="line355"></a>
<a name="line356"></a>  this.events_.length = 0;
<a name="line357"></a>  this.outstandingEvents_.clear();
<a name="line358"></a>  this.startTime_ = goog.debug.Trace_.now();
<a name="line359"></a>  this.tracerOverheadStart_ = 0;
<a name="line360"></a>  this.tracerOverheadEnd_ = 0;
<a name="line361"></a>  this.tracerOverheadComment_ = 0;
<a name="line362"></a>  this.tracerCount_ = 0;
<a name="line363"></a>  this.commentCount_ = 0;
<a name="line364"></a>
<a name="line365"></a>  var keys = this.stats_.getKeys();
<a name="line366"></a>  for (var i = 0; i &lt; keys.length; i++) {
<a name="line367"></a>    var key = keys[i];
<a name="line368"></a>    var stat = this.stats_.get(key);
<a name="line369"></a>    stat.count = 0;
<a name="line370"></a>    stat.time = 0;
<a name="line371"></a>    stat.varAlloc = 0;
<a name="line372"></a>    this.statPool_.releaseObject(/** @type {Object} */ (stat));
<a name="line373"></a>  }
<a name="line374"></a>  this.stats_.clear();
<a name="line375"></a>};
<a name="line376"></a>
<a name="line377"></a>
<a name="line378"></a>/**
<a name="line379"></a> * Starts a tracer
<a name="line380"></a> * @param {string} comment A comment used to identify the tracer. Does not
<a name="line381"></a> *     need to be unique.
<a name="line382"></a> * @param {string=} opt_type Type used to identify the tracer. If a Trace is
<a name="line383"></a> *     given a type (the first argument to the constructor) and multiple Traces
<a name="line384"></a> *     are done on that type then a &quot;TOTAL line will be produced showing the
<a name="line385"></a> *     total number of traces and the sum of the time
<a name="line386"></a> *     (&quot;TOTAL Database 2 (37 ms)&quot; in our example). These traces should be
<a name="line387"></a> *     mutually exclusive or else the sum won&#39;t make sense (the time will
<a name="line388"></a> *     be double counted if the second starts before the first ends).
<a name="line389"></a> * @return {number} The identifier for the tracer that should be passed to the
<a name="line390"></a> *     the stopTracer method.
<a name="line391"></a> */
<a name="line392"></a>goog.debug.Trace_.prototype.startTracer = function(comment, opt_type) {
<a name="line393"></a>  var tracerStartTime = goog.debug.Trace_.now();
<a name="line394"></a>  var varAlloc = this.getTotalVarAlloc();
<a name="line395"></a>  var outstandingEventCount = this.outstandingEvents_.getCount();
<a name="line396"></a>  if (this.events_.length + outstandingEventCount &gt; this.MAX_TRACE_SIZE) {
<a name="line397"></a>    this.logger_.warning(&#39;Giant thread trace. Clearing to &#39; +
<a name="line398"></a>                         &#39;avoid memory leak.&#39;);
<a name="line399"></a>    // This is the more likely case. This usually means that we
<a name="line400"></a>    // either forgot to clear the trace or else we are performing a
<a name="line401"></a>    // very large number of events
<a name="line402"></a>    if (this.events_.length &gt; this.MAX_TRACE_SIZE / 2) {
<a name="line403"></a>      for (var i = 0; i &lt; this.events_.length; i++) {
<a name="line404"></a>        var event = this.events_[i];
<a name="line405"></a>        if (event.id) {
<a name="line406"></a>          this.idPool_.releaseObject(event.id);
<a name="line407"></a>        }
<a name="line408"></a>        this.eventPool_.releaseObject(event);
<a name="line409"></a>      }
<a name="line410"></a>      this.events_.length = 0;
<a name="line411"></a>    }
<a name="line412"></a>
<a name="line413"></a>    // This is less likely and probably indicates that a lot of traces
<a name="line414"></a>    // aren&#39;t being closed. We want to avoid unnecessarily clearing
<a name="line415"></a>    // this though in case the events do eventually finish.
<a name="line416"></a>    if (outstandingEventCount &gt; this.MAX_TRACE_SIZE / 2) {
<a name="line417"></a>      this.outstandingEvents_.clear();
<a name="line418"></a>    }
<a name="line419"></a>  }
<a name="line420"></a>
<a name="line421"></a>  goog.debug.Logger.logToProfilers(&#39;Start : &#39; + comment);
<a name="line422"></a>
<a name="line423"></a>  var event = /** @type {goog.debug.Trace_.Event_} */ (
<a name="line424"></a>      this.eventPool_.getObject());
<a name="line425"></a>  event.totalVarAlloc = varAlloc;
<a name="line426"></a>  event.eventType = goog.debug.Trace_.EventType.START;
<a name="line427"></a>  event.id = Number(this.idPool_.getObject());
<a name="line428"></a>  event.comment = comment;
<a name="line429"></a>  event.type = opt_type;
<a name="line430"></a>  this.events_.push(event);
<a name="line431"></a>  this.outstandingEvents_.set(String(event.id), event);
<a name="line432"></a>  this.tracerCount_++;
<a name="line433"></a>  var now = goog.debug.Trace_.now();
<a name="line434"></a>  event.startTime = event.eventTime = now;
<a name="line435"></a>  this.tracerOverheadStart_ += now - tracerStartTime;
<a name="line436"></a>  return event.id;
<a name="line437"></a>};
<a name="line438"></a>
<a name="line439"></a>
<a name="line440"></a>/**
<a name="line441"></a> * Stops a tracer
<a name="line442"></a> * @param {number|undefined|null} id The id of the tracer that is ending.
<a name="line443"></a> * @param {number=} opt_silenceThreshold Threshold below which the tracer is
<a name="line444"></a> *    silenced.
<a name="line445"></a> * @return {?number} The elapsed time for the tracer or null if the tracer
<a name="line446"></a> *    identitifer was not recognized.
<a name="line447"></a> */
<a name="line448"></a>goog.debug.Trace_.prototype.stopTracer = function(id, opt_silenceThreshold) {
<a name="line449"></a>  // this used to call goog.isDef(opt_silenceThreshold) but that causes an
<a name="line450"></a>  // object allocation in IE for some reason (doh!). The following code doesn&#39;t
<a name="line451"></a>  // cause an allocation
<a name="line452"></a>  var now = goog.debug.Trace_.now();
<a name="line453"></a>  var silenceThreshold;
<a name="line454"></a>  if (opt_silenceThreshold === 0) {
<a name="line455"></a>    silenceThreshold = 0;
<a name="line456"></a>  } else if (opt_silenceThreshold) {
<a name="line457"></a>    silenceThreshold = opt_silenceThreshold;
<a name="line458"></a>  } else {
<a name="line459"></a>    silenceThreshold = this.defaultThreshold_;
<a name="line460"></a>  }
<a name="line461"></a>
<a name="line462"></a>  var startEvent = this.outstandingEvents_.get(String(id));
<a name="line463"></a>  if (startEvent == null) {
<a name="line464"></a>    return null;
<a name="line465"></a>  }
<a name="line466"></a>
<a name="line467"></a>  this.outstandingEvents_.remove(String(id));
<a name="line468"></a>
<a name="line469"></a>  var stopEvent;
<a name="line470"></a>  var elapsed = now - startEvent.startTime;
<a name="line471"></a>  if (elapsed &lt; silenceThreshold) {
<a name="line472"></a>    var count = this.events_.length;
<a name="line473"></a>    for (var i = count - 1; i &gt;= 0; i--) {
<a name="line474"></a>      var nextEvent = this.events_[i];
<a name="line475"></a>      if (nextEvent == startEvent) {
<a name="line476"></a>        this.events_.splice(i, 1);
<a name="line477"></a>        this.idPool_.releaseObject(startEvent.id);
<a name="line478"></a>        this.eventPool_.releaseObject(/** @type {Object} */ (startEvent));
<a name="line479"></a>        break;
<a name="line480"></a>      }
<a name="line481"></a>    }
<a name="line482"></a>
<a name="line483"></a>  } else {
<a name="line484"></a>    stopEvent = /** @type {goog.debug.Trace_.Event_} */ (
<a name="line485"></a>        this.eventPool_.getObject());
<a name="line486"></a>    stopEvent.eventType = goog.debug.Trace_.EventType.STOP;
<a name="line487"></a>    stopEvent.startTime = startEvent.startTime;
<a name="line488"></a>    stopEvent.comment = startEvent.comment;
<a name="line489"></a>    stopEvent.type = startEvent.type;
<a name="line490"></a>    stopEvent.stopTime = stopEvent.eventTime = now;
<a name="line491"></a>
<a name="line492"></a>    this.events_.push(stopEvent);
<a name="line493"></a>  }
<a name="line494"></a>
<a name="line495"></a>  var type = startEvent.type;
<a name="line496"></a>  var stat = null;
<a name="line497"></a>  if (type) {
<a name="line498"></a>    stat = this.getStat_(type);
<a name="line499"></a>    stat.count++;
<a name="line500"></a>    stat.time += elapsed;
<a name="line501"></a>  }
<a name="line502"></a>  if (stopEvent) {
<a name="line503"></a>    goog.debug.Logger.logToProfilers(&#39;Stop : &#39; + stopEvent.comment);
<a name="line504"></a>
<a name="line505"></a>    stopEvent.totalVarAlloc = this.getTotalVarAlloc();
<a name="line506"></a>
<a name="line507"></a>    if (stat) {
<a name="line508"></a>      stat.varAlloc += (stopEvent.totalVarAlloc - startEvent.totalVarAlloc);
<a name="line509"></a>    }
<a name="line510"></a>  }
<a name="line511"></a>  var tracerFinishTime = goog.debug.Trace_.now();
<a name="line512"></a>  this.tracerOverheadEnd_ += tracerFinishTime - now;
<a name="line513"></a>  return elapsed;
<a name="line514"></a>};
<a name="line515"></a>
<a name="line516"></a>
<a name="line517"></a>/**
<a name="line518"></a> * Sets the ActiveX object that can be used to get GC tracing in IE6.
<a name="line519"></a> * @param {Object} gcTracer GCTracer ActiveX object.
<a name="line520"></a> */
<a name="line521"></a>goog.debug.Trace_.prototype.setGcTracer = function(gcTracer) {
<a name="line522"></a>  this.gcTracer_ = gcTracer;
<a name="line523"></a>};
<a name="line524"></a>
<a name="line525"></a>
<a name="line526"></a>/**
<a name="line527"></a> * Returns the total number of allocations since the GC stats were reset. Only
<a name="line528"></a> * works in IE.
<a name="line529"></a> * @return {number} The number of allocaitons or -1 if not supported.
<a name="line530"></a> */
<a name="line531"></a>goog.debug.Trace_.prototype.getTotalVarAlloc = function() {
<a name="line532"></a>  var gcTracer = this.gcTracer_;
<a name="line533"></a>  // isTracing is defined on the ActiveX object.
<a name="line534"></a>  if (gcTracer &amp;&amp; gcTracer[&#39;isTracing&#39;]()) {
<a name="line535"></a>    return gcTracer[&#39;totalVarAlloc&#39;];
<a name="line536"></a>  }
<a name="line537"></a>  return -1;
<a name="line538"></a>};
<a name="line539"></a>
<a name="line540"></a>
<a name="line541"></a>/**
<a name="line542"></a> * Adds a comment to the trace. Makes it possible to see when a specific event
<a name="line543"></a> * happened in relation to the traces.
<a name="line544"></a> * @param {string} comment A comment that is inserted into the trace.
<a name="line545"></a> * @param {?string=} opt_type Type used to identify the tracer. If a comment is
<a name="line546"></a> *     given a type and multiple comments are done on that type then a &quot;TOTAL
<a name="line547"></a> *     line will be produced showing the total number of comments of that type.
<a name="line548"></a> * @param {?number=} opt_timeStamp The timestamp to insert the comment. If not
<a name="line549"></a> *    specified, the current time wil be used.
<a name="line550"></a> */
<a name="line551"></a>goog.debug.Trace_.prototype.addComment = function(comment, opt_type,
<a name="line552"></a>                                                  opt_timeStamp) {
<a name="line553"></a>  var now = goog.debug.Trace_.now();
<a name="line554"></a>  var timeStamp = opt_timeStamp ? opt_timeStamp : now;
<a name="line555"></a>
<a name="line556"></a>  var eventComment = /** @type {goog.debug.Trace_.Event_} */ (
<a name="line557"></a>      this.eventPool_.getObject());
<a name="line558"></a>  eventComment.eventType = goog.debug.Trace_.EventType.COMMENT;
<a name="line559"></a>  eventComment.eventTime = timeStamp;
<a name="line560"></a>  eventComment.type = opt_type;
<a name="line561"></a>  eventComment.comment = comment;
<a name="line562"></a>  eventComment.totalVarAlloc = this.getTotalVarAlloc();
<a name="line563"></a>  this.commentCount_++;
<a name="line564"></a>
<a name="line565"></a>  if (opt_timeStamp) {
<a name="line566"></a>    var numEvents = this.events_.length;
<a name="line567"></a>    for (var i = 0; i &lt; numEvents; i++) {
<a name="line568"></a>      var event = this.events_[i];
<a name="line569"></a>      var eventTime = event.eventTime;
<a name="line570"></a>
<a name="line571"></a>      if (eventTime &gt; timeStamp) {
<a name="line572"></a>        goog.array.insertAt(this.events_, eventComment, i);
<a name="line573"></a>        break;
<a name="line574"></a>      }
<a name="line575"></a>    }
<a name="line576"></a>    if (i == numEvents) {
<a name="line577"></a>      this.events_.push(eventComment);
<a name="line578"></a>    }
<a name="line579"></a>  } else {
<a name="line580"></a>    this.events_.push(eventComment);
<a name="line581"></a>  }
<a name="line582"></a>
<a name="line583"></a>  var type = eventComment.type;
<a name="line584"></a>  if (type) {
<a name="line585"></a>    var stat = this.getStat_(type);
<a name="line586"></a>    stat.count++;
<a name="line587"></a>  }
<a name="line588"></a>
<a name="line589"></a>  this.tracerOverheadComment_ += goog.debug.Trace_.now() - now;
<a name="line590"></a>};
<a name="line591"></a>
<a name="line592"></a>
<a name="line593"></a>/**
<a name="line594"></a> * Gets a stat object for a particular type. The stat object is created if it
<a name="line595"></a> * hasn&#39;t yet been.
<a name="line596"></a> * @param {string} type The type of stat.
<a name="line597"></a> * @return {goog.debug.Trace_.Stat_} The stat object.
<a name="line598"></a> * @private
<a name="line599"></a> */
<a name="line600"></a>goog.debug.Trace_.prototype.getStat_ = function(type) {
<a name="line601"></a>  var stat = this.stats_.get(type);
<a name="line602"></a>  if (!stat) {
<a name="line603"></a>    stat = /** @type {goog.debug.Trace_.Event_} */ (
<a name="line604"></a>        this.statPool_.getObject());
<a name="line605"></a>    stat.type = type;
<a name="line606"></a>    this.stats_.set(type, stat);
<a name="line607"></a>  }
<a name="line608"></a>  return /** @type {goog.debug.Trace_.Stat_} */(stat);
<a name="line609"></a>};
<a name="line610"></a>
<a name="line611"></a>
<a name="line612"></a>/**
<a name="line613"></a> * Returns a formatted string for the current trace
<a name="line614"></a> * @return {string} A formatted string that shows the timings of the current
<a name="line615"></a> *     trace.
<a name="line616"></a> */
<a name="line617"></a>goog.debug.Trace_.prototype.getFormattedTrace = function() {
<a name="line618"></a>  return this.toString();
<a name="line619"></a>};
<a name="line620"></a>
<a name="line621"></a>
<a name="line622"></a>/**
<a name="line623"></a> * Returns a formatted string that describes the thread trace.
<a name="line624"></a> * @return {string} A formatted string.
<a name="line625"></a> * @override
<a name="line626"></a> */
<a name="line627"></a>goog.debug.Trace_.prototype.toString = function() {
<a name="line628"></a>  var sb = [];
<a name="line629"></a>  var etime = -1;
<a name="line630"></a>  var indent = [];
<a name="line631"></a>  for (var i = 0; i &lt; this.events_.length; i++) {
<a name="line632"></a>    var e = this.events_[i];
<a name="line633"></a>    if (e.eventType == goog.debug.Trace_.EventType.STOP) {
<a name="line634"></a>      indent.pop();
<a name="line635"></a>    }
<a name="line636"></a>    sb.push(&#39; &#39;, e.toTraceString(this.startTime_, etime, indent.join(&#39;&#39;)));
<a name="line637"></a>    etime = e.eventTime;
<a name="line638"></a>    sb.push(&#39;\n&#39;);
<a name="line639"></a>    if (e.eventType == goog.debug.Trace_.EventType.START) {
<a name="line640"></a>      indent.push(&#39;|  &#39;);
<a name="line641"></a>    }
<a name="line642"></a>  }
<a name="line643"></a>
<a name="line644"></a>  if (this.outstandingEvents_.getCount() != 0) {
<a name="line645"></a>    var now = goog.debug.Trace_.now();
<a name="line646"></a>
<a name="line647"></a>    sb.push(&#39; Unstopped timers:\n&#39;);
<a name="line648"></a>    goog.iter.forEach(this.outstandingEvents_, function(startEvent) {
<a name="line649"></a>      sb.push(&#39;  &#39;, startEvent, &#39; (&#39;, now - startEvent.startTime,
<a name="line650"></a>          &#39; ms, started at &#39;,
<a name="line651"></a>          goog.debug.Trace_.formatTime_(startEvent.startTime),
<a name="line652"></a>          &#39;)\n&#39;);
<a name="line653"></a>    });
<a name="line654"></a>  }
<a name="line655"></a>
<a name="line656"></a>  var statKeys = this.stats_.getKeys();
<a name="line657"></a>  for (var i = 0; i &lt; statKeys.length; i++) {
<a name="line658"></a>    var stat = this.stats_.get(statKeys[i]);
<a name="line659"></a>    if (stat.count &gt; 1) {
<a name="line660"></a>      sb.push(&#39; TOTAL &#39;, stat, &#39;\n&#39;);
<a name="line661"></a>    }
<a name="line662"></a>  }
<a name="line663"></a>
<a name="line664"></a>  sb.push(&#39;Total tracers created &#39;, this.tracerCount_, &#39;\n&#39;,
<a name="line665"></a>      &#39;Total comments created &#39;, this.commentCount_, &#39;\n&#39;,
<a name="line666"></a>      &#39;Overhead start: &#39;, this.tracerOverheadStart_, &#39; ms\n&#39;,
<a name="line667"></a>      &#39;Overhead end: &#39;, this.tracerOverheadEnd_, &#39; ms\n&#39;,
<a name="line668"></a>      &#39;Overhead comment: &#39;, this.tracerOverheadComment_, &#39; ms\n&#39;);
<a name="line669"></a>
<a name="line670"></a>  return sb.join(&#39;&#39;);
<a name="line671"></a>};
<a name="line672"></a>
<a name="line673"></a>
<a name="line674"></a>/**
<a name="line675"></a> * Converts &#39;v&#39; to a string and pads it with up to 3 spaces for
<a name="line676"></a> * improved alignment. TODO there must be a better way
<a name="line677"></a> * @param {number} v A number.
<a name="line678"></a> * @return {string} A padded string.
<a name="line679"></a> * @private
<a name="line680"></a> */
<a name="line681"></a>goog.debug.Trace_.longToPaddedString_ = function(v) {
<a name="line682"></a>  v = Math.round(v);
<a name="line683"></a>  // todo (pupius) - there should be a generic string in goog.string for this
<a name="line684"></a>  var space = &#39;&#39;;
<a name="line685"></a>  if (v &lt; 1000) space = &#39; &#39;;
<a name="line686"></a>  if (v &lt; 100) space = &#39;  &#39;;
<a name="line687"></a>  if (v &lt; 10) space = &#39;   &#39;;
<a name="line688"></a>  return space + v;
<a name="line689"></a>};
<a name="line690"></a>
<a name="line691"></a>
<a name="line692"></a>/**
<a name="line693"></a> * Return the sec.ms part of time (if time = &quot;20:06:11.566&quot;,  &quot;11.566
<a name="line694"></a> * @param {number} time The time in MS.
<a name="line695"></a> * @return {string} A formatted string as sec.ms&#39;.
<a name="line696"></a> * @private
<a name="line697"></a> */
<a name="line698"></a>goog.debug.Trace_.formatTime_ = function(time) {
<a name="line699"></a>  time = Math.round(time);
<a name="line700"></a>  var sec = (time / 1000) % 60;
<a name="line701"></a>  var ms = time % 1000;
<a name="line702"></a>
<a name="line703"></a>  // TODO their must be a nicer way to get zero padded integers
<a name="line704"></a>  return String(100 + sec).substring(1, 3) + &#39;.&#39; +
<a name="line705"></a>         String(1000 + ms).substring(1, 4);
<a name="line706"></a>};
<a name="line707"></a>
<a name="line708"></a>
<a name="line709"></a>/**
<a name="line710"></a> * Returns the current time. Done through a wrapper function so it can be
<a name="line711"></a> * overridden by application code. Gmail has an ActiveX extension that provides
<a name="line712"></a> * higher precision timing info.
<a name="line713"></a> * @return {number} The current time in milliseconds.
<a name="line714"></a> */
<a name="line715"></a>goog.debug.Trace_.now = function() {
<a name="line716"></a>  return goog.now();
<a name="line717"></a>};
<a name="line718"></a>
<a name="line719"></a>
<a name="line720"></a>/**
<a name="line721"></a> * Singleton trace object
<a name="line722"></a> * @type {goog.debug.Trace_}
<a name="line723"></a> */
<a name="line724"></a>goog.debug.Trace = new goog.debug.Trace_();
</pre>


</body>
</html>
